home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / RELEASE.ZIP / sub_arctic / test / constraint_patterns.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  9.6 KB  |  324 lines

  1.  
  2. package sub_arctic.test;
  3.  
  4. import sub_arctic.lib.*;
  5. import sub_arctic.constraints.std_function;
  6. import sub_arctic.constraints.constraint;
  7. import sub_arctic.input.callback_object;
  8. import sub_arctic.input.event;
  9. import sub_arctic.output.drawable;
  10.  
  11. import java.awt.Color;
  12. import java.awt.Font;
  13. import java.awt.FontMetrics;
  14.  
  15. /** 
  16.  * Applet to do a demo of various common constraints.
  17.  * @author Scott Hudson
  18.  */
  19. public class constraint_patterns extends debug_interactor_applet 
  20.   implements callback_object, cycle_handler {
  21.  
  22.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  23.  
  24.   labeled_object parent_obj, prev_sibling, obj, next_sibling;
  25.   label message1, message2, message3;
  26.   text_toggle_collection x_select, w_select;
  27.   scale a_slider;
  28.  
  29.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  30.  
  31.   /** Initialization of sub_arctic interface when applet starts */
  32.   public void build_ui(base_parent_interactor top) 
  33.     {
  34.       Font lab_font = new Font("Helvetica",Font.BOLD,12);
  35.  
  36.       /* build overall column */
  37.       column ui_col = new column(2,column.LEFT_JUSTIFIED);
  38.       ui_col.set_pos(10,10);
  39.       top.add_child(ui_col);
  40.  
  41.       /* Put a title label in */
  42.       ui_col.add_child(new label("A Selection of Commonly Used Constraints", 
  43.                  new Font("Helvetica", Font.BOLD, 16)));
  44.  
  45.       /* build our sample objects */
  46.       parent_obj   = new labeled_object(350,175,"parent_obj");
  47.       prev_sibling = new labeled_object(100,100,"prev_sibling");
  48.       next_sibling = new labeled_object(60,100,"next_sibling");
  49.       obj          = new labeled_object(100,100,"obj");
  50.       a_slider     = new scale(5,0, 250, 10,200, 100, 10, null);
  51.  
  52.       /* put them in our test hierarchy */
  53.       parent_obj.add_child(prev_sibling);
  54.       prev_sibling.set_x(10);
  55.       prev_sibling.set_y(20);
  56.       parent_obj.add_child(obj);
  57.       obj.set_x(75);
  58.       obj.set_y(40);
  59.       parent_obj.add_child(next_sibling);
  60.       next_sibling.set_x(270);
  61.       next_sibling.set_y(20);
  62.       a_slider.set_y_constraint(std_function.far_edge_just(PARENT.Y2(), 5));
  63.       parent_obj.add_child(a_slider);
  64.       ui_col.add_child(parent_obj);
  65.  
  66.       /* build rest of interface around it */
  67.  
  68.       /* messages */
  69.       message1 = new label("No cycles detected", lab_font);
  70.       ui_col.add_child(message1);
  71.  
  72.       /* x constraint selector */
  73.       ui_col.add_child(new label("Set obj.x constraint to:", lab_font));
  74.       x_select = new text_toggle_collection(x_select_data, true, -1, this);
  75.       x_select.nth_toggle(0).set_cur_state(1);
  76.       ui_col.add_child(x_select);
  77.  
  78.       /* w constraint selector */
  79.       ui_col.add_child(new label("Set obj.w constraint to:", lab_font));
  80.       w_select = new text_toggle_collection(w_select_data, true, -1, this);
  81.       w_select.nth_toggle(0).set_cur_state(1);
  82.       ui_col.add_child(w_select);
  83.  
  84.       /* setup to catch cycles in constraints with callback to us */
  85.       manager.handle_cycles_with(manager.EXCEPTION_CUSTOM, this);
  86.  
  87.     }
  88.  
  89.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  90.  
  91.   String[] x_select_data = {
  92.     "obj.x <= [No constraint] 75", 
  93.     "obj.x <= [Next to sibling] std_function.offset(PREV_SIBLING.X2(), 5)", 
  94.     "obj.x <= [Parent left edge] std_function.offset(PARENT.X(), 5)",
  95.     "obj.x <= [Parent right edge] std_function.far_edge_just(PARENT.X2(), 5)",
  96.     "obj.x <= [Centered in parent] std_function.centered(PARENT.W(), 0)"
  97.   };
  98.  
  99.   String[] w_select_data = {
  100.     "obj.w <= [No constraint] 100", 
  101.     "obj.w <= [Fill to parent] "+
  102.           "std_function.fill(SELF.X(), PARENT.X2(), 5)",
  103.     "obj.w <= [Fill to next sib] " +
  104.               "std_function.fill(SELF.X(), NEXT_SIBLING.X(), 5)",
  105.     "obj.w <= [Set by slider] " +
  106.           "std_function.eq(OTHER.OBJ(a_slider).PART_A())"
  107.   };
  108.  
  109.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  110.  
  111.   /** Handle callbacks. */
  112.   public void callback(interactor from, event evt, int cb_num, Object cb_parm) 
  113.     {
  114.       int obj_val;
  115.  
  116.       /* reset the cycle message (will be set in cycle handler if we have one)*/
  117.       message1.set_text("No cycles detected");
  118.  
  119.       if (from instanceof label_toggle)
  120.     {
  121.           /* is this the x constraint selection */
  122.           if (from.parent() == x_select)
  123.         {
  124.           /* extract the toggle number and act on it */
  125.               obj_val=((Integer)from.user_info()).intValue();
  126.           switch (obj_val)
  127.             {
  128.               case 0: 
  129.             obj.set_x_constraint(NO_CONSTRAINT); 
  130.             obj.set_x(75);
  131.           break;
  132.               case 1: 
  133.             obj.set_x_constraint(std_function.offset(PREV_SIBLING.X2(),5));
  134.             break;
  135.               case 2: 
  136.             obj.set_x_constraint(std_function.offset(PARENT.X(), 5)); 
  137.           break;
  138.               case 3: 
  139.             obj.set_x_constraint(
  140.               std_function.far_edge_just(PARENT.X2(),5)); 
  141.           break;
  142.               case 4: 
  143.             obj.set_x_constraint(std_function.centered(PARENT.W(), 0)); 
  144.           break;
  145.               default:
  146.             message1.set_text("UNKNOWN SETTING FOR OBJ.X!!");
  147.               break;
  148.             }
  149.  
  150.           /* hit the width to make sure we expose any cycles */
  151.           obj.mark_w_ood();
  152.         }
  153.       /* is thsi the w constraint selection */
  154.           else if (from.parent() == w_select)
  155.         {
  156.           /* extract the toggle number and act on it */
  157.               obj_val=((Integer)from.user_info()).intValue();
  158.           switch (obj_val)
  159.             {
  160.               case 0: 
  161.             obj.set_w_constraint(NO_CONSTRAINT); 
  162.             obj.set_w(100);
  163.           break;
  164.               case 1: 
  165.             obj.set_w_constraint(
  166.               std_function.fill(SELF.X(), PARENT.X2(), 5)); 
  167.           break;
  168.               case 2: 
  169.             obj.set_w_constraint(
  170.               std_function.fill(SELF.X(), NEXT_SIBLING.X(), 5));
  171.               break;
  172.           case 3: 
  173.             obj.set_w_constraint(
  174.               std_function.eq(OTHER.OBJ(a_slider).PART_A()));
  175.           break;
  176.               default:
  177.             message1.set_text("UNKNOWN SETTING FOR OBJ.W!!");
  178.               break;
  179.             }
  180.  
  181.           /* hit x to make sure we expose any cycles */
  182.           obj.mark_x_ood();
  183.         }
  184.     }
  185.     }
  186.  
  187.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  188.  
  189.   /** Handle cycle_detection */
  190.   public boolean handle_cycle(interactor in_obj, int part_code)
  191.     {
  192.       /* change the message then continue with evaluation anyway */
  193.       message1.set_text("CYCLE DETECTED!!");
  194.       return true;
  195.     }
  196.  
  197.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  198. }
  199.  
  200. /*---------------------------------------------------------------------*/
  201.  
  202. /** 
  203.  * Small interactor class that draws a rectangle for its bounding box along with
  204.  * a small label at the top left.
  205.  * @author Scott Hudson
  206.  */
  207. class labeled_object extends base_parent_interactor {
  208.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  209.  
  210.   /** 
  211.    * Constructor 
  212.    * @param int    wid width of the object.
  213.    * @param int    hi  height of the object.
  214.    * @param String lab text of the display label
  215.    */
  216.   public labeled_object(int wid, int hi, String lab)  
  217.     {
  218.       super(0,0,wid,hi);
  219.       set_text(lab);
  220.       set_font(new Font("Helvetica",Font.PLAIN,9));
  221.     }
  222.  
  223.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  224.  
  225.   /** Text for the label we display */
  226.   protected String _text = "";
  227.  
  228.   /** 
  229.    * Text for the label we display 
  230.    * @return String the string we are currently displaying.
  231.    */
  232.   public String text() {return _text;}
  233.  
  234.   /** 
  235.    * Set the text for the label we display 
  236.    * @param String t the new label text.
  237.    */
  238.   public void set_text(String t) 
  239.     {
  240.       if (!_text.equals(t))
  241.     {
  242.           _text = t;
  243.           damage_self();
  244.     }
  245.     }
  246.  
  247.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  248.  
  249.  
  250.   /** Font to display label in */
  251.   protected Font _font = null;
  252.  
  253.   /** Metrics object for current font */
  254.   protected FontMetrics _metrics = null;
  255.  
  256.   /** 
  257.    * Font to display label in 
  258.    * @return Font the font we are display in.
  259.    */
  260.   public Font font() {return _font;}
  261.  
  262.   /** 
  263.    * Set font to display label in 
  264.    * @param Font f the new font.
  265.    */
  266.   public void set_font(Font f) 
  267.     {
  268.       if (_font != f)
  269.     {
  270.           _font = f;
  271.       _metrics = manager.get_metrics(f);
  272.           damage_self();
  273.     }
  274.     }
  275.  
  276.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  277.  
  278.   /** 
  279.    * Output routine for the interactor.
  280.    * @param drawable surf the drawing surface we draw on.
  281.    */
  282.   public void draw_self_local(drawable surf)
  283.     {
  284.       int sz_w, sz_h;
  285.  
  286.       /* draw a rectangle at the bounds */
  287.       surf.setColor(Color.black);
  288.       surf.drawRect(0,0, w()-1, h()-1);
  289.  
  290.       /* measure the string and clear behind it */
  291.       sz_w = _metrics.stringWidth(text()) + 4;
  292.       sz_h = _metrics.getHeight() + 2;
  293.       surf.setColor(Color.white);
  294.       surf.fillRect(0,0, sz_w-1,sz_h-1);
  295.  
  296.       /* draw the text tag */
  297.       surf.setColor(Color.black);
  298.       surf.setFont(font());
  299.       surf.drawString(text(), 2, _metrics.getAscent()+1);
  300.       surf.drawRect(0,0, sz_w-1, sz_h-1);
  301.  
  302.       /* let superclass draw any children */
  303.       super.draw_self_local(surf);
  304.     }
  305.  
  306.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  307. }
  308. /*=========================== COPYRIGHT NOTICE ===========================
  309.  
  310. This file is part of the subArctic user interface toolkit.
  311.  
  312. Copyright (c) 1996 Scott Hudson and Ian Smith
  313. All rights reserved.
  314.  
  315. The subArctic system is freely available for most uses under the terms
  316. and conditions described in 
  317.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  318. and appearing in full in the lib/interactor.java source file.
  319.  
  320. The current release and additional information about this software can be 
  321. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  322.  
  323. ========================================================================*/
  324.